/**
 * 加载事件
 * chrome open
 * open -a 'Google Chrome' --args -allow-file-access-from-files
 *  --disable-web-security
 */
window.addEventListener('load', function() {
	new FastClick(document.body);
}, false);

var slider = new PageSlider($("#container"));
$(window).on('hashchange', Global.route);

document.addEventListener("backbutton", function(e){
	var hash = window.location.hash;
	var params = Global.getParameters(hash);
	var id = params['#id'];
	if ('guidepage' == id || 'marketpage' == id || 'parterpage' == id || 'userpage' == id) {
		navigator.app.exitApp();
	} else {
		navigator.app.backHistory();
	}
}, false);
/**
 * 页面加载完毕的事件
 */
document.addEventListener('pageloaded', function(e) {
	window.setTimeout(function() {
		console.log('pageloaded event.' + e.detail.length);
		var content = e.detail.find('div.content:first');
		if (content.length > 0 && content.attr('page-nobanner')) {
			return;
		}
		var html = $('body').data('_globabannerdata');
		var header = e.detail.find('div.header:first');
		if (html) {
			GlobalBanner(html, header, content);
			return;
		}
		$.ajax({
			type: 'get',
			url: GLOBAL_CONTEXT_PATH + '/adbanner/adbannerJson.html?_=' + new Date().getTime(),
			success: function(data) {
				console.log('Banner count:' + data.list.length);
				if (data.list && data.list.length > 0) {
					var adbanner = $('<div class="adbanner"></div>');
					adbanner.html(Global.loadFragment('adbannerlistitems', data));
					$('body').data('_globabannerdata', adbanner);
					GlobalBanner(adbanner, header, content);
				}
			}
		});
	}, 0);
});

function GlobalBanner(adbanner, header, content) {
	window.setTimeout(function() {
		header.append(adbanner);
		content.css({
			marginTop: '87px'
		});
	}, 0);
	var interval = $('body').data('_globalinterval');
	if (interval) {
		clearInterval(interval);
	}
	interval = setInterval(function() {
		var li = adbanner.find('li.ui-mybanner-link');
		if (li.length <= 1) {
			return;
		}
		var cs = adbanner.find('li.ui-mybanner-link:visible');
		var next = cs.next('li');
		if (next.length == 0) {
			next = li.first();
		}
		li.hide();
		next.show('slow');
	}, 5 * 1000);
	$('body').data('_globalinterval', interval);
}

/**
 * 修改异步请求的部分信息
 */
$.ajaxSetup({
	beforeSend: function(xhr) {
		xhr.setRequestHeader('X-Requested-With', 'appjson');
	},
	statusCode: {
		307: function(params) {
			//params.readyState/getResponseHeader/getAllResponseHeaderssetRequestHeader
			//overrideMimeType/abort/state/always/then/promise/pipe/done/fail/progress
			//complete/success/error/responseText/status/statusText
			if (Global.loginInfo) {
				console.log('系统正在验证身份，请稍等...');
				var idcode = params.getResponseHeader('ID-Code');
				console.debug('ID-Code:' + idcode);
				var password = Global.loginInfo.password;
				var username = Global.loginInfo.username;
				$.ajax({
					type: 'POST',
			    	url: GLOBAL_CONTEXT_PATH + '/login/loginjson.html?_=' + new Date().getTime(),
			    	dataType: 'json',
			    	data: {
			    		'person.username': username,
			    		'person.password': password,
			    		'identifyCode': idcode
			    	},
			    	success: function (data) {
			    		if (data && data.login) {
			    			Global.setItem(Global.loginKey, data.userMap);
			    			data.userMap.password = password;
			    			Global.loginInfo = data.userMap;
				    		window.location.hash = '#userpage';
				    		console.log('身份验证成功');
				    		Global.longToast('登录成功');
			    		} else {
			    			console.log('身份验证失败');
			    			window.location.hash = '#userlogin';
				    		Global.longToast('登录失败:' + data.mapValue.msg);
			    		}
			    	}
				});
			}
		}
	},
	complete: function(jqxhr, status) {
		
//		if (status == 307) {
//			if (Global.loginInfo) {
//				var idcode = jqxhr.getRequestHeader();
//			}
//		}
	}
});
//广告的地址/adbanner/adbannerJson.html
$(document).ajaxStart(function() {
	console.log('=======>ajaxStart');
}).ajaxComplete(function(event, XMLHttpRequest, ajaxOptions) {
	console.log('=======>ajaxComplete');
}).ajaxStop(function() {
	console.log('=======>ajaxStop');
});

function imgLoadError(img) {
	img.src = 'images/MORE.png';
}

function userRegister() {
	window.location.hash = '#userregister';
}

//iphone打电话
//window.open('tel:12345678', '_system')
